package com.ptl.bp;

import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamFileFinder {
    public static   List<Path> findFiles(String dirPath, String regex) throws IOException {
        Pattern pattern = Pattern.compile(regex);

        try (Stream<Path> paths = Files.walk(Paths.get(dirPath))) {
           return paths.filter(Files::isRegularFile)
                    .filter(path -> pattern.matcher(path.toString()).matches())
                    .collect(Collectors.toList());
        }
    }

    public static void copyFiles(List<Path> sourcePaths, Path targetDir) throws IOException {
        // 确保目标目录存在（如果不存在则创建）
        Files.createDirectories(targetDir);

        sourcePaths.forEach(source -> {
            try {
                Path target = targetDir.resolve(source.getFileName());
                Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
                System.out.println("Copied: " + source + " to " + target);
            } catch (IOException e) {
                System.err.println("Failed to copy " + source + ": " + e.getMessage());
            }
        });
    }

    public static void main(String[] args) throws IOException {
        String directory = "D:\\workspace\\ptl-电池护照\\Docs";
        String regex = ".*\\\\1.2.0.*payload.json$"; // 查找src/main/java目录下的所有Java文件
        List<Path> paths = findFiles(directory, regex);
        copyFiles(paths, Paths.get("D:\\workspace\\ptl-电池护照\\BpBackend\\src\\main\\resources\\schema2"));
    }

}